package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.practice.ArrayPractice;
import org.liaohailong.helloworld.practice.DPPractice;
import org.liaohailong.helloworld.practice.LinkedPractice;
import org.liaohailong.helloworld.practice.SSPractice;
import org.liaohailong.helloworld.practice.StringPractice;
import org.liaohailong.helloworld.practice.TreePractice;

import java.util.Arrays;
import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/5/12 09:46
 * Describe:
 */
public class Practice1 implements AUtil.ILog {

    @Test
    public void removeDuplicates() {
        ArrayPractice ap = new ArrayPractice();
        int[] nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int count = ap.removeDuplicates(nums);
        log("count = " + count + ", num = " + Arrays.toString(nums));
    }

    @Test
    public void maxProfit() {
        ArrayPractice ap = new ArrayPractice();
        // [7,1,5,3,6,4] = 7
        // [7,6,4,3,1] = 0
        // [1,2,3,4,5] = 4
//        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
//        int[] prices = new int[]{7,6,4,3,1};
//        int[] prices = new int[]{1, 2, 3, 4, 5};
        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
        int profit = ap.maxProfit(prices);
        log("profit = " + profit);
    }

    @Test
    public void rotate() {
        ArrayPractice ap = new ArrayPractice();
//        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};
//        int k = 3;

        int[] nums = new int[]{-1, -100, 3, 99};
        int k = 2;
        ap.rotate(nums, k);
        log(Arrays.toString(nums));
    }

    @Test
    public void intersect() {
        ArrayPractice ap = new ArrayPractice();
//        int[] nums1 = new int[]{4, 9, 5};
//        int[] nums2 = new int[]{9, 4, 9, 8, 4};
        int[] nums1 = new int[]{9, 3, 7};
        int[] nums2 = new int[]{6, 4, 1, 0, 0, 4, 4, 8, 7};
        int[] intersect = ap.intersect(nums1, nums2);
        log(Arrays.toString(intersect));
    }

    @Test
    public void plusOne() {
        ArrayPractice ap = new ArrayPractice();
//        int[] digits = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        int[] digits = new int[]{9, 9, 9};
        int[] ints = ap.plusOne(digits);
        log(Arrays.toString(ints));
    }

    @Test
    public void rotateMatrix() {
        ArrayPractice ap = new ArrayPractice();
        int[][] matrix = new int[4][4];
        matrix[0] = new int[]{1, 2, 3, 4};
        matrix[1] = new int[]{5, 6, 7, 8};
        matrix[2] = new int[]{9, 10, 11, 12};
        matrix[3] = new int[]{13, 14, 15, 16};
        ap.rotate(matrix);
        for (int[] ints : matrix) log(Arrays.toString(ints));
    }

    @Test
    public void reverse() {
        StringPractice sp = new StringPractice();
//        int x = 120;
//        int x = 0;
        int x = -123;
        int reverse = sp.reverse(x);
        log(String.valueOf(reverse));
    }

    @Test
    public void isAnagram() {
        StringPractice sp = new StringPractice();
//        String s = "anagram";
//        String t = "nagaram";
        String s = "car";
        String t = "rat";
        boolean anagram = sp.isAnagram(s, t);
        log("anagram = " + anagram);
    }

    @Test
    public void isPalindrome() {
        StringPractice sp = new StringPractice();
        String s = "A man, a plan, a canal: Panama";
//        String s = "0P";
        boolean palindrome = sp.isPalindrome(s);
        log("palindrome = " + palindrome);
    }

    @Test
    public void myAtoi() {
        StringPractice sp = new StringPractice();
//        String s = "42";
//        String s = "   -42";
//        String s = "4193 with words";
//        String s = "words and 987";
//        String s = "-91283472332";
//        String s = "3.14159";
//        String s = "   +0 123";
//        String s = "20000000000000000000";
//        String s = "  0000000000012345678";
//        String s = "00000-42a1234";
//        String s = "000000000000000000";
//        String s = ".1";
//        String s = "-000000000000001";
//        String s = "+1";
//        String s = "-1";
        String s = "-2147483648";
        int i = sp.myAtoi(s);
        log(" i = " + i);
    }

    @Test
    public void strStr() {
        StringPractice sp = new StringPractice();
        String haystack = "hello";
        String needle = "ll";

//        String haystack = "a";
//        String needle = "a";

//        String haystack = "aaa";
//        String needle = "aaaa";

//        String haystack = "mississippi";
//        String needle = "issipi";
        int index = sp.strStr(haystack, needle);
        log("index = " + index);
    }

    @Test
    public void countAndSay() {
        StringPractice sp = new StringPractice();
        String s = sp.countAndSay(5);
        log(s);
    }

    @Test
    public void longestCommonPrefix() {
        StringPractice sp = new StringPractice();
//        String[] str = {"flower","flow","flight"};
        String[] str = {"dog", "racecar", "car"};
        String s = sp.longestCommonPrefix(str);
        log(s);
    }

    @Test
    public void removeNthFromEnd() {
        LinkedPractice lp = new LinkedPractice();
//        int[] arr = new int[]{1, 2, 3, 4, 5};
//        int n = 2;

//        int[] arr = new int[]{1};
//        int n = 1;

        int[] arr = new int[]{1, 2};
        int n = 1;

        LinkedPractice.ListNode head = LinkedPractice.create(arr);
        LinkedPractice.ListNode res = lp.removeNthFromEnd(head, n);
        LinkedPractice.print(res);
    }

    @Test
    public void reverseList() {
        LinkedPractice lp = new LinkedPractice();
        int[] arr = new int[]{1, 2, 3, 4, 5};
        LinkedPractice.ListNode head = LinkedPractice.create(arr);
        LinkedPractice.ListNode res = lp.reverseList(head);
        LinkedPractice.print(res);
    }

    @Test
    public void mergeTwoLists() {
        LinkedPractice lp = new LinkedPractice();
        int[] arr1 = new int[]{1, 3, 5, 7, 9};
        int[] arr2 = new int[]{2, 4, 6, 8, 10};
        LinkedPractice.ListNode head1 = LinkedPractice.create(arr1);
        LinkedPractice.ListNode head2 = LinkedPractice.create(arr2);
        LinkedPractice.ListNode res = lp.mergeTwoLists(head1, head2);
        LinkedPractice.print(res);
    }

    @Test
    public void isPalindromeList() {
        LinkedPractice lp = new LinkedPractice();
        int[] arr = new int[]{1, 2, 2, 1};
        LinkedPractice.ListNode head = LinkedPractice.create(arr);
        boolean palindrome = lp.isPalindrome(head);
        LinkedPractice.print(head);
        log("palindrome = " + palindrome);
    }

    @Test
    public void maxDepth() {
        Integer[] arr = new Integer[]{3, 9, 20, null, null, 15, 7};
        TreePractice.TreeNode root = TreePractice.create(arr);
        TreePractice tp = new TreePractice();
        int depth = tp.maxDepth(root);
        log("depth = " + depth);
    }

    @Test
    public void isValidBST() {
//        Integer[] arr = new Integer[]{5, 1, 4, null, null, 3, 6};
//        Integer[] arr = new Integer[]{2, 1, 3};
        Integer[] arr = new Integer[]{4, 5, 6, null, null, 3, 7};
        TreePractice.TreeNode root = TreePractice.create(arr);
        TreePractice tp = new TreePractice();
        boolean validBST = tp.isValidBST(root);
        log("validBST = " + validBST);
    }

    @Test
    public void levelOrder() {
        Integer[] arr = new Integer[]{3, 9, 20, null, null, 15, 7};
        TreePractice.TreeNode root = TreePractice.create(arr);
        TreePractice tp = new TreePractice();
        List<List<Integer>> levelOrder = tp.levelOrder(root);
        log(levelOrder.toString());
    }

    @Test
    public void firstBadVersion() {
        SSPractice ssp = new SSPractice();
        int i = ssp.firstBadVersion(2126753390);
        log("i = " + i);
    }

    @Test
    public void climbStairs() {
        DPPractice dp = new DPPractice();
        for (int i = 1; i <= 10; i++) {
            int climbStairs = dp.climbStairs(i);
            log("i = " + i + ", climbStairs = " + climbStairs);
        }
    }

    public void log(String msg) {
        log("Exam1", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
